<?php
namespace App\Services\Article\Article;

use App\Models\Article\ArticleClassify;
use App\Services\Log\Log\LogLogic;

/**
 * 文章分类逻辑层
 * Class ArticleclassifyLogic
 * @package App\Services\Article\Article
 */
class ArticleclassifyLogic
{
    /**
     * 文章主题添加
     * @return array
     */
    public function add($data)
    {
        $data['created_at'] = date('Y-m-d H:i:s', time());
        $data['updated_at'] = date('Y-m-d H:i:s', time());

        $rst = ArticleClassify::insertGetId($data);
        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 1;
            $logData['record'] = "文章主题";
            (new LogLogic())->addLog($logData);
        }

        return auto_return(['id' => $rst],'分类添加成功！', '文章分类添加失败');
    }

    /**
     * 文章主题修改
     *  @return array
     */
    public function classifyEdit($data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s', time());
        $rst = ArticleClassify::where('id',$data['id'])->update($data);

        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 2;
            $logData['record'] = "文章主题";
            (new LogLogic())->addLog($logData);
        }
        return auto_return(['id' => $rst],'主题信息保存成功！', '分类信息保存失败');
    }

    /**
     * 获取文章全部主题
     * @return array
     */
    public function classifyList()
    {
        $condition['status'] = 1;
        $res = ArticleClassify::where($condition)->paginate(10);

        return auto_return($res);
    }

    /**
     * 获取APP文章全部分类列表
     * @return array
     */
    public function appClassifyList()
    {
        $condition['status'] = 1;
        $condition['belong_to'] = 1;
        $arr = ArticleClassify::where($condition)->get()->toArray();
        $list = _make_tree($arr);
        return auto_return($list);
    }

    /**
     * 获取文章全部主题(不分页)
     * @return array
     */
    public function themeList()
    {
        $condition['status'] = 1;

        $res = ArticleClassify::where($condition)->get();

        return auto_return($res);
    }

    /**
     * 获取文章主题信息
     */
    public function classifyInfo($data)
    {
        $res = ArticleClassify::where($data)->first();

        return auto_return($res);
    }
}
